package com.javazyw.utils.lang;

import java.io.IOException;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.javazyw.entity.bo.Constant;
import com.javazyw.entity.bo.ResponseEnum;

/**
 * <p>fastjson工具类.</p>
 *
 * @version 
 * @author dongdh
 * @date 2015年5月20日
 *
 */
public class JSONUtils {

	public static SimplePropertyPreFilter excludesProperty(String[] excludes) {
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
		Set<String>  set = filter.getExcludes();
		
		for (int i = 0; i < excludes.length; i++) {
			set.add(excludes[i]);
		}
		return filter;
	}
	
	/*public static SimplePropertyPreFilter includesProperty(String[] includes) {
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
		Set<String>  set = filter.getIncludes();
		
		for (int i = 0; i < includes.length; i++) {
			set.add(includes[i]);
		}
		return filter;
	}*/
	
	public static SimplePropertyPreFilter includesProperty(Class clazz, String...includes) {
		SimplePropertyPreFilter filter = new SimplePropertyPreFilter(clazz, includes);
		return filter;
	}
	
	public static SerializeConfig addDateLongFormatConfig() {
		SerializeConfig config = new SerializeConfig();
		return addDateLongFormatConfig(config);
	}
	
	public static SerializeConfig addDateLongFormatConfig(SerializeConfig config) {
		config.put(java.util.Date.class, new JSONDataFormatSerializer());
		return config;
	}
	
	public static SerializeConfig addDateFormatConfig(String pattern) {
		SerializeConfig config = new SerializeConfig();
		config.put(java.util.Date.class, new JSONDataFormatSerializer(pattern));
		return config;
	}

	public static JSONObject returnStatus(String status) {
		JSONObject json = new JSONObject();
		json.put(Constant.RESPONSE_STATUS_KEY,status);
		json.put(Constant.RESPONSE_MSG_KEY, ResponseEnum.get(status).getMsg());
		return json;
	}

    public static JSONObject returnStatus( String status,String message){
        JSONObject json = new JSONObject();
        json.put(Constant.RESPONSE_STATUS_KEY, status);
        json.put(Constant.RESPONSE_MSG_KEY, message);
        return json;
    }

    public static JSONObject returnStatus( String status,Object data){
        JSONObject json = new JSONObject();
        json.put(Constant.RESPONSE_STATUS_KEY, status);
        json.put(Constant.RESPONSE_DATA_KEY, data);
        return json;
    }
	
	/**
	 * 
	 * @param json
	 * @return
	 */
	public static JSONObject paramEmptyError(JSONObject json) {
		json.put(Constant.RESPONSE_STATUS_KEY, ResponseEnum.RESPONSE_PARAM_EMPTY.getStatus());
		json.put(Constant.RESPONSE_MSG_KEY, ResponseEnum.RESPONSE_PARAM_EMPTY.getMsg());
		return json;
	}
	
	/**
	 * 初始对象 设置为成功
	 * @return
	 */
	public static JSONObject getInitJsonObj() {
		JSONObject json = new JSONObject();
		json.put(Constant.RESPONSE_STATUS_KEY, ResponseEnum.RESPONSE_SUCCESS.getStatus());
		json.put(Constant.RESPONSE_MSG_KEY, ResponseEnum.RESPONSE_SUCCESS.getMsg());
		return json;
	}
	
	/**
	 * 服务端异常json
	 * @param json
	 * @return
	 */
	public static JSONObject errorJsonObj(JSONObject json) {
		json.put(Constant.RESPONSE_STATUS_KEY, ResponseEnum.RESPONSE_SERVER_ERROR.getStatus());
		json.put(Constant.RESPONSE_MSG_KEY, ResponseEnum.RESPONSE_SERVER_ERROR.getMsg());
		return json;
	}

    private static JSONObject putData(Object data,String []excludesPropertys,JSONObject json){
        SimplePropertyPreFilter filter = JSONUtils.excludesProperty(excludesPropertys);
        SerializeConfig config = JSONUtils.addDateLongFormatConfig();
        String jsonstr = JSONArray.toJSONString(data, config, filter);
        json.put(Constant.RESPONSE_DATA_KEY, JSONArray.parse(jsonstr));
        return json;
    }

    public static JSONObject returnStatus(String status,Object data,String []excludesPropertys,Map<String,Object> params){
        JSONObject jsonObject=returnStatus(status);
        JSONObject tempJson=JSONObject.parseObject(JSONObject.toJSONString(data));
        //判断参数是否为空
        if(EmptyUtils.isNotEmpty(params)){
            for(Map.Entry<String, Object> entry:params.entrySet()){
                tempJson.put(entry.getKey(),entry.getValue());
            }
        }
        return putData(tempJson,excludesPropertys,jsonObject);
    }
}

class JSONDataFormatSerializer implements ObjectSerializer {

	String pattern;
	public JSONDataFormatSerializer(String pattern) {
		this.pattern = pattern;
	}
	
	public JSONDataFormatSerializer() {
	}
	
	///参考   JSONLibDataFormatSerializer
	@Override
	public void write(JSONSerializer serializer, Object object,
			Object fieldName, Type fieldType, int features) throws IOException {
		
		if (object == null) {
    		serializer.getWriter().writeNull();
    		return;
    	}
		Date date = (Date) object;
		if (pattern != null && !"".equals(pattern)) {
			DateFormat df = new SimpleDateFormat(pattern);
			 serializer.write(df.format(date));
		} else {
	        serializer.write(date.getTime());
		}
	}




}
